package engine.ui
{
	import engine.GameData;
	import engine.control.GlowTween;
	import engine.control.NewGolw;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.GameDefine;
	import engine.logic.loading.LoadingLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.BottomIndexUI;
	import engine.newUi.RightTopIndexUI;
	import engine.newUi.TopPlayerUI;
	
	import flash.display.DisplayObject;
	
	import newx.com.config.StaticIni;

	public class MainLogic
	{
		private static var _self	:	MainLogic;
		
		public static function getInstance():MainLogic
		{
			if (_self == null)
			{
				_self = new MainLogic();
			}
			return _self;
		}
		
		private	var _isInit				:Boolean;
		
		private	var	_topPlayerUI		:TopPlayerUI;
		private 	var _bottomIndexUI		:BottomIndexUI;

		private	var	_model				:ModelLocator;
		private	var	_bindManage			:BindManage;
		private	var	_sceneViewType		:int;
		private	var	_titleStr			:String	=	StaticIni.
			getSwfLangStr("MessageUI_Notice");
		//等级
		private 	var _level				:int;
		//发光
		private    var newGolwtween		:GlowTween;
		private var target:Object=new Object();
		private var newgolw:NewGolw;
		public static var isGolw:Boolean=false;
		
		/**当前消息字符串ID**/
		private var _currMsgStrID:String;
		public function MainLogic()
		{
			init();
		}
		private function init():void
		{
			newgolw=new NewGolw();

			_model		=	ModelLocator.getInstance();
			_bindManage	=	BindManage.getInstance();
			
			addListener();
			
			_topPlayerUI		=	TopPlayerUI.getInstance();
			_bottomIndexUI		=	BottomIndexUI.getInstance();
		}
		
		//添加事件
		private function addListener():void
		{
			//通知客户端界面发光 uiId
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_UIHOTLIGHT), uiHotLight);
			
			//通用消息
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SHOWMESSAGEBOX),
				onShowCommonAlert);
		}
		
		//获得场景名称
		private function getSceneName():String
		{
			return getNormalSceneName();
		}
		
		//获得普通场景名称
		private function getNormalSceneName():String
		{
			var modelID		:	int 			=	GameData.SceneModel;
			var configID	:	int 			=	GameData.SceneConfig;
			
			if(0 != configID)
			{
				return	StaticIni.getSwfLangStr("scenename_" + configID);
			}else{
				return	StaticIni.getSwfLangStr("scenename_" + modelID);
			}
		}
			
		//收到新邮件，邮箱显示。
		private function receiveMail(e:CustomMsgEvent):void
		{
//			_mainTopIndexUI.mailEffectPlay();
		}
		
		//技能/背包按钮发光
		private function uiHotLight(e:CustomMsgEvent):void
		{
			var index:int = e.pList[0];

//			_topPlayerUI.setIconGline(e.pList[0]);
			showGlowTween(index);
			
		}
		private function showGlowTween(index:int):void
		{
		
			var targetUI:String;
			var obj:Object = StaticIni.getIniObj("UIRegedit");
			for(var i:String in obj)
			{
				if(i=="UI_"+index.toString())
				{
					targetUI =obj[i];
				}
			}
			switch(targetUI)
			{
				case "MailUI":
					if(RightTopIndexUI.getInstance().bt2.visible==true)
					{
					   RightTopIndexUI.getInstance().mailglow.visible=true;
					   RightTopIndexUI.getInstance().mailglow.play();
					}
					
					isGolw=true;
				
						
					
//					target["mc"]=RightTopIndexUI.getInstance().bt2;
					break;
			}
//			if(target["mc"])
//			{
//				newgolw.setMc(target["mc"]);
//				newgolw.StarPlay(target["mc"]);
////				newGolwtween.startGlowHandler(target["mc"]);
//			}
					
		}
		public function stopGlow(target:DisplayObject):void
		{
			newgolw.StopPlay(target);
//			newGolwtween.stopGlowHandler(target);
			
		}
		/**
		 *当前MessageBox消息的字符串ID 
		 * @return 
		 * 
		 */		
		public function get currMsgStrID():String
		{
			return _currMsgStrID
		}
		/**
		 * 通知客户端弹出一个MessageBox:
		 * p0		：类型
		 * p1		：返回个数
		 * p2		：返回数据
		 * p3...N	：消息字符串填充。
		 *
		 */
		private function onShowCommonAlert(e:CustomMsgEvent):void
		{
			var type:int			=	e.pList.shift()
			var returnNum:int		=	e.pList.shift()
			var returnData:Array	=	[]
			var infoArray:Array		=	[];
			var	titleStr:String	=	StaticIni.getSwfLangStr("MessageUI_Notice");
			var i : int;
			for ( i = 0; i < returnNum; i++)
			{
				returnData.push(e.pList.shift());
			}
			var msgStrID:String	=	e.pList.shift();
			_currMsgStrID =msgStrID;
			infoArray				=	e.pList;
			
			for( i = 0;  i < infoArray.length; i ++ )
			{
				if( infoArray[ i ] is int ) continue;
				infoArray[ i ] = StaticIni.getSwfLangStr( infoArray[ i ] );
			}
			
			if (type == GameDefine.MESSAGEBOX_NONE)
			{
					AlertUI.showAlert(
						titleStr,
						StaticIni.getSwfLangStrVar(msgStrID,infoArray),
						AlertUI.OK);
			}
			else 
			{
					AlertUI.showAlert(
						titleStr,
						StaticIni.getSwfLangStrVar(msgStrID,infoArray),
						AlertUI.YES|AlertUI.NO,alertClick,returnData);
			}
			LoadingLogic.getInstance().hide();
		}
		
		private function alertClick(clickName:uint,inputTxt:String,clickData:Array):void
		{
			var answer:int = 0;
			if(clickName==AlertUI.YES)
			{
				answer = 1;
			}
			else if(clickName==AlertUI.NO)
			{
				answer = 0;
			}
			//插入玩家选择的返回结果
			clickData.unshift(answer);
			SystemSender.getInstance().CustomMsgEx(CustomToServerFlag.CLIENT_CUSTOMMSG_ANSWERMESSAGEBOX,clickData);
		}
		/**
		 *检查容器是不是已经满了 
		 * @param viewID
		 * @param lenProperty
		 * @return 
		 * 
		 */		
		public function checkViewFull(viewID:int,lenProperty:String):Boolean
		{
			var view:View=_model.scene.GetView(viewID);
			if(!view)return false;
			var obj:Object=view.Properties;
			var currentLen:int;
			for each (var i:Object in obj) 
			{
				if(i is ViewObject)currentLen++;
			}
			var maxLen:int=view.GetPropertiesEx(lenProperty);
			return maxLen<=currentLen;
		}
	}
}